const { Router } = require('express')
const express = require('express')
//路由容器
const index = express.Router()
const axios = require('axios')
const qs = require('qs')
const jwt = require("jsonwebtoken")
//面试者管理页面
index.get('/', (req, res) => {
    res.render('admin/index',({userInfo:jwt.decode(req.session.token)}))
})
//渲染面试者管理页面
index.get('/findAllInterviews', (req, res) => {
    // const {pageNow, pageSize}=req.query
    // if (pageNow == undefined || pageSize == undefined){
    //     console.log(req.query)
    //     return res.status(400).render('admin/error', {mag:'参数错误'})
    // } else{
        axios({
            url: 'http://1.117.92.175:8081/user/findAllInterviews',
            method: 'GET',
            // params: {
            //     pageNow: pageNow,
            //     pageSize: pageSize
            // }
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.send(data.data)
            // console.log(data.data)
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
            // console.log('err' + 'err');
        })
    // }

})

//删除单个面试者
index.get('/deleteInterview', (req, res) => {
    const {_id} = req.query
    // console.log(_id)
    if (_id == undefined) {
        return res.status(400).render('admin/error', {msg:'参数错误'})
    } else {
        axios({
            url:'http://1.117.92.175:8081/deleteInterviewer',
            method:'DELETE',
            params: {
                id: _id
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.send(data.data)
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
            // console.log('err' + 'err');
        })
    }
})
//批量删除面试者
index.get('/deleteAllInterview', (req, res) => {
    const {_id} = req.query
    if (_id == undefined) {
        return res.status(400).render('admin/error', {msg: 参数错误})
    } else {
        axios ({
            url:'http://1.117.92.175:8081/batchDeleteInterviewer',
            method:'DELETE',
            params: {
                id: _id
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
            paramsSerializer: params => {
                return qs.stringify(params, { indices: false })
            }
        })
        .then(data => {
            if (data.data>0) {
                res.send({err:0,data:data.data,msg:"删除成功"})
            }else{
                res.send({err:-1,data:data.data,msg:"删除失败"})
            }
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
            // console.log('err' + 'err')
        })
    }
})

//修改面试者的状态
index.get('/changeState', (req, res) => {
    const {state,studentId} = req.query
    console.log(state);
    if (state == undefined || studentId == undefined) {
        return res.status(400).render('admin/error', {msg: 参数错误})
    } else {
        axios ({
            url: 'http://1.117.92.175:8081/user/changeState',
            method: 'POST',
            params: {
                state: state,
                studentId: studentId
            },
            headers:{
                token:req.session.token?req.session.token:"",
            }
        })
        .then(data => {
            // console.log(data.data);
            res.send(data.data)
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
            // console.log('err' + 'err');
        })
       
    }
})

//根据姓名查找面试者
index.get('/findByNameLike', (req, res) => {
    const {name,choose} = req.query
    console.log(req.query);
    if (name == undefined) {
        return res.status(400).render('admin/error', {msg: 参数错误})
    } else {
        axios ({
            // url:'http://1.117.92.175:8081/findByNameLike',
            url:'ttp://1.117.92.175:8081/user/FuzzyQuery',
            method:'GET',
            params:{
                // name:name
                choose:choose,
                like:name
            },
            headers: {
                token: req.session.token?req.session.token:""
            }
        })
        .then(data => {
            // console.log(data.data);
            res.send(data.data)
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
            // console.log('err' + 'err');
        })
    }
})

//筛选不同状态面试者
index.get('/findInterviewersByState', (req, res) => {
    const {state} = req.query
    if (state == undefined) {
        return res.status(400).render('admin/error')
    } else {
        axios({
            url:'http://1.117.92.175:8081/findInterviewersByState',
            method: 'GET',
            params:{
                state: state
            },
            headers: {
                token: req.session.token?req.session.token:""
            }
        })
        .then(data => {
            res.send(data.data)
        })
        .catch(err => {
            // console.log(err);
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
    }
})

//查看单个面试人员信息
index.get('/findByStudentId', (req, res) => {
    const {studentId} = req.query
    if(studentId == undefined) {
        return res.status(400).render('admin/error')
    } else {
        axios({
            url:'http://1.117.92.175:8081/user/findByStudentId',
            method: 'GET',
            params:{
                studentId:studentId
            },
            headers:{
                token: req.session.token?req.session.token:""
            }
        })
        .then(data => {
            res.send(data.data)
        })
        .catch(err => {
            // console.log(err);
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
    }
})

// 增加面试者
index.get('/addUsersUrl', (req, res) => {
    const {name, classes,sex,studentId,Telephone,qq}=req.query;
    console.log(req.query);
    axios({
        url:"http://1.117.92.175:8081/addInterviewers",
        method:"POST",
        params: {
            name:name,
            classes:classes,
            sex:sex,
            studentId:studentId,
            Telephone:Telephone,
            qq:qq,
        },
        headers:{
            token: req.session.token?req.session.token:""
        }
    })
    .then(data=>{
        console.log(data.data);
        res.send(data.data)
    //    console.log(data.data);
    })
    .catch(err=>{
        res.send(err)
        console.log("err "+"err");
    })
})
//修改面试者信息
index.get('/updateInterviewer', (req, res) => {
    const {name, classes,sex,studentId,Telephone,qq,interviewersId}=req.query;
    console.log(req.query);
    axios({
        url:"http://1.117.92.175:8081/updateInterviewer",
        method:"POST",
        params: {
            name:name,
            id: interviewersId,
            classes:classes,
            sex:sex,
            studentId:studentId,
            telephone:Telephone,
            qq:qq,
        },
        headers:{
            token: req.session.token?req.session.token:""
        }
    })
    .then(data=>{
        console.log(data.data);
        res.send(data.data)
    //    console.log(data.data);
    })
    .catch(err=>{
        res.send(err)
        console.log("err "+err);
    })
})
//批量修改状态
index.get('/changeStateMore', (req, res) => {
    const {state,ids}=req.query;
    console.log(req.query);
    axios({
        url:"http://1.117.92.175:8081/user/BatchEdit",
        method:"POST",
        params: {
            state:state,
            ids:ids
        },  
        headers:{
            token: req.session.token?req.session.token:""
        }
    })
    .then(data=>{
        console.log(data.data);
        res.send(data.data)
    //    console.log(data.data);
    })
    .catch(err=>{
        res.send(err)
        console.log("err "+err);
    })
})
module.exports=index;
